/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Processing.GameOption;
import Processing.SaveManager;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;

/**
 * 
 */
public class MyChessMidlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    public GameOption option = new GameOption();
    private Hashtable FileSaveList = null;
    private boolean isPlaying = false;
    public Player m_playSound = null;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form formMHChinh;
    private ChoiceGroup choiceGame;
    private Spacer spacer;
    private ChoiceGroup choiceP1;
    private Spacer spacer1;
    private ChoiceGroup choiceP2;
    private Form formLoad;
    private ChoiceGroup choiceFileSave;
    private Form formAbout;
    private Spacer spacer2;
    private StringItem stringItem;
    private StringItem stringItem2;
    private StringItem stringItem1;
    private List list;
    private Command OK_ChonCheDoChoi;
    private Command BACK_ChonAbout;
    private Command BACK_ChonOption;
    private Command BACK_ChonLoad;
    private Command OK_ChonOption;
    private Command OK_ChonLoad;
    private Image image2;
    private Image image1;
    private Font font1;
    //</editor-fold>//GEN-END:|fields|0|

    private MainForm m_MainForm = null;

    /**
     * The MyChessMidlet constructor.
     */
    public MyChessMidlet() {

    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
        SaveManager.DeleteAll();
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
        //getDisplay().setCurrent(new MainForm());
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        if(nextDisplayable == list)
        {
            isPlaying = false;
        }
        else if(nextDisplayable == m_MainForm)
        {
            isPlaying = true;
        }
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
        PlaySound();
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formMHChinh ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of formMHChinh component.
     * @return the initialized component instance
     */
    public Form getFormMHChinh() {
        if (formMHChinh == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            formMHChinh = new Form("Ch\u1ECDn ch\u1EBF \u0111\u1ED9 ch\u01A1i", new Item[] { getChoiceGame(), getSpacer(), getChoiceP1(), getSpacer1(), getChoiceP2() });//GEN-BEGIN:|14-getter|1|14-postInit
            formMHChinh.addCommand(getBACK_ChonOption());
            formMHChinh.addCommand(getOK_ChonOption());
            formMHChinh.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return formMHChinh;
    }
    //</editor-fold>//GEN-END:|14-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGame ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initiliazed instance of choiceGame component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGame() {
        if (choiceGame == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            choiceGame = new ChoiceGroup("", Choice.EXCLUSIVE);//GEN-BEGIN:|17-getter|1|17-postInit
            choiceGame.append("C\u1EDD vua", getImage1());
            choiceGame.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_NEWLINE_AFTER);
            choiceGame.setSelectedFlags(new boolean[] { true });
            choiceGame.setFont(0, getFont1());//GEN-END:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return choiceGame;
    }
    //</editor-fold>//GEN-END:|17-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|20-getter|0|20-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|20-getter|1|20-@java.io.IOException
                image1 = Image.createImage("/Chess_12.png");
            } catch (java.io.IOException e) {//GEN-END:|20-getter|1|20-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|20-getter|2|20-postInit
            // write post-init user code here
        }//GEN-BEGIN:|20-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|20-getter|3|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image2 ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of image2 component.
     * @return the initialized component instance
     */
    public Image getImage2() {
        if (image2 == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|21-getter|1|21-@java.io.IOException
                image2 = Image.createImage("/Chess_12.png");
            } catch (java.io.IOException e) {//GEN-END:|21-getter|1|21-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|21-getter|2|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|3|
        return image2;
    }
    //</editor-fold>//GEN-END:|21-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font1 ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of font1 component.
     * @return the initialized component instance
     */
    public Font getFont1() {
        if (font1 == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            font1 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return font1;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == formAbout) {//GEN-BEGIN:|7-commandAction|1|55-preAction
            if (command == BACK_ChonAbout) {//GEN-END:|7-commandAction|1|55-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|2|55-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|57-preAction
        } else if (displayable == formLoad) {
            if (command == BACK_ChonLoad) {//GEN-END:|7-commandAction|3|57-preAction
                // write pre-action user code here
                if(isPlaying)
                {
                    switchDisplayable(null, m_MainForm);
                    return;
                }
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|4|57-postAction
                // write post-action user code here
            } else if (command == OK_ChonLoad) {//GEN-LINE:|7-commandAction|5|67-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|6|67-postAction
                // write post-action user code here
                String nameChon = choiceFileSave.getString(choiceFileSave.getSelectedIndex());
                Integer idFile = (Integer)FileSaveList.get(nameChon);
                SaveManager.OpenDB();
                byte[] dl = SaveManager.Read(idFile.intValue());
                SaveManager.CloseDB();
                option.ChessType = dl[0];
                option.BlackPlayerType = dl[1];
                option.WhitePlayerType = dl[2];
                m_MainForm = new MainForm(this, dl);
                isPlaying = true;
                switchDisplayable(null, m_MainForm);
            }//GEN-BEGIN:|7-commandAction|7|59-preAction
        } else if (displayable == formMHChinh) {
            if (command == BACK_ChonOption) {//GEN-END:|7-commandAction|7|59-preAction
                // write pre-action user code here
                if(isPlaying)
                {
                    switchDisplayable(null, m_MainForm);
                    return;
                }
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|8|59-postAction
                // write post-action user code here
            } else if (command == OK_ChonOption) {//GEN-LINE:|7-commandAction|9|63-preAction
                // write pre-action user code here
                option.ChessType = choiceGame.getSelectedIndex();
                option.BlackPlayerType = choiceP1.getSelectedIndex();
                option.WhitePlayerType = choiceP2.getSelectedIndex();
                m_MainForm = new MainForm(this);
                isPlaying = true;
                switchDisplayable(null, m_MainForm);
//GEN-LINE:|7-commandAction|10|63-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|46-preAction
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|11|46-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|7-commandAction|12|46-postAction
                // write post-action user code here
            } else if (command == OK_ChonCheDoChoi) {//GEN-LINE:|7-commandAction|13|50-preAction
                // write pre-action user code here
                switch(list.getSelectedIndex())
                {
                    case 0:
                    {
                        switchDisplayable(null, getFormMHChinh());
                        break;
                    }
                    case 1:
                    {
                        switchDisplayable(null, newFormLoad());
                        break;
                    }
                    case 2:
                    {
                        switchDisplayable(null, getFormAbout());
                        break;
                    }
                    case 3:
                    {
                        exitMIDlet();
                        break;
                    }
                }
//GEN-LINE:|7-commandAction|14|50-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|7-postCommandAction
        }//GEN-END:|7-commandAction|15|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|16|
    //</editor-fold>//GEN-END:|7-commandAction|16|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OK_ChonCheDoChoi ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of OK_ChonCheDoChoi component.
     * @return the initialized component instance
     */
    public Command getOK_ChonCheDoChoi() {
        if (OK_ChonCheDoChoi == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            OK_ChonCheDoChoi = new Command("Ok", Command.OK, 0);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return OK_ChonCheDoChoi;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of spacer component.
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 3);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return spacer;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceP1 ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of choiceP1 component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceP1() {
        if (choiceP1 == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            choiceP1 = new ChoiceGroup("Player 1", Choice.EXCLUSIVE);//GEN-BEGIN:|28-getter|1|28-postInit
            choiceP1.append("Ng\u01B0\u1EDDi ch\u01A1i", null);
            choiceP1.append("M\u00E1y ch\u01A1i", null);
            choiceP1.setSelectedFlags(new boolean[] { true, false });//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return choiceP1;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of spacer1 component.
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 1);//GEN-LINE:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return spacer1;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceP2 ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of choiceP2 component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceP2() {
        if (choiceP2 == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            choiceP2 = new ChoiceGroup("Player 2", Choice.EXCLUSIVE);//GEN-BEGIN:|32-getter|1|32-postInit
            choiceP2.append("Ng\u01B0\u1EDDi ch\u01A1i", null);
            choiceP2.append("M\u00E1y ch\u01A1i", null);
            choiceP2.setSelectedFlags(new boolean[] { false, true });//GEN-END:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return choiceP2;
    }
    //</editor-fold>//GEN-END:|32-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formLoad ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initiliazed instance of formLoad component.
     * @return the initialized component instance
     */
    public Form getFormLoad() {
        if (formLoad == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            formLoad = new Form("Load game", new Item[] { getChoiceFileSave() });//GEN-BEGIN:|36-getter|1|36-postInit
            formLoad.addCommand(getBACK_ChonLoad());
            formLoad.addCommand(getOK_ChonLoad());
            formLoad.setCommandListener(this);//GEN-END:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return formLoad;
    }
    //</editor-fold>//GEN-END:|36-getter|2|

    public Form newFormLoad()
    {
        formLoad = new Form("Load game", new Item[] { newChoiceFileSave() });
        formLoad.addCommand(getBACK_ChonLoad());
        formLoad.addCommand(getOK_ChonLoad());
        formLoad.setCommandListener(this);
        return formLoad;
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formAbout ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of formAbout component.
     * @return the initialized component instance
     */
    public Form getFormAbout() {
        if (formAbout == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            formAbout = new Form("About", new Item[] { getSpacer2(), getStringItem(), getStringItem1(), getStringItem2() });//GEN-BEGIN:|37-getter|1|37-postInit
            formAbout.addCommand(getBACK_ChonAbout());
            formAbout.setCommandListener(this);//GEN-END:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return formAbout;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer2 ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of spacer2 component.
     * @return the initialized component instance
     */
    public Spacer getSpacer2() {
        if (spacer2 == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            spacer2 = new Spacer(16, 1);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return spacer2;
    }
    //</editor-fold>//GEN-END:|38-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            stringItem = new StringItem("J2ME chess", "");//GEN-BEGIN:|39-getter|1|39-postInit
            stringItem.setPreferredSize(150, -1);
            stringItem.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER | Item.LAYOUT_2);//GEN-END:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Sinh vi\u00EAn 1:", "0612176 - Phan Duy H\u00F9ng");//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|41-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem2 ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of stringItem2 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem2() {
        if (stringItem2 == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            stringItem2 = new StringItem("Sinh vi\u00EAn 2:", "0612416 - Ho\u00E0ng Xu\u00E2n Th\u1EA3o");//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return stringItem2;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of list component.
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            isPlaying = false;
            list = new List("Game \u0111\u00E1nh c\u1EDD", Choice.IMPLICIT);//GEN-BEGIN:|44-getter|1|44-postInit
            list.append("New game", null);
            list.append("Load game", null);
            list.append("About", null);
            list.append("Exit game", null);
            list.addCommand(getOK_ChonCheDoChoi());
            list.setCommandListener(this);
            list.setFitPolicy(Choice.TEXT_WRAP_ON);
            list.setSelectCommand(getOK_ChonCheDoChoi());
            list.setSelectedFlags(new boolean[] { true, false, false, false });
            list.setFont(0, getFont1());
            list.setFont(1, getFont1());
            list.setFont(2, getFont1());
            list.setFont(3, getFont1());//GEN-END:|44-getter|1|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|2|
        return list;
    }
    //</editor-fold>//GEN-END:|44-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|44-action|0|44-preAction
    /**
     * Performs an action assigned to the selected list element in the list component.
     */
    public void listAction() {//GEN-END:|44-action|0|44-preAction
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|44-action|1|48-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("New game")) {//GEN-END:|44-action|1|48-preAction
                // write pre-action user code here
//GEN-LINE:|44-action|2|48-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Load game")) {//GEN-LINE:|44-action|3|51-preAction
                // write pre-action user code here
//GEN-LINE:|44-action|4|51-postAction
                // write post-action user code here
            } else if (__selectedString.equals("About")) {//GEN-LINE:|44-action|5|52-preAction
                // write pre-action user code here
//GEN-LINE:|44-action|6|52-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Exit game")) {//GEN-LINE:|44-action|7|53-preAction
                // write pre-action user code here
//GEN-LINE:|44-action|8|53-postAction
                // write post-action user code here
            }//GEN-BEGIN:|44-action|9|44-postAction
        }//GEN-END:|44-action|9|44-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|44-action|10|
    //</editor-fold>//GEN-END:|44-action|10|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BACK_ChonAbout ">//GEN-BEGIN:|54-getter|0|54-preInit
    /**
     * Returns an initiliazed instance of BACK_ChonAbout component.
     * @return the initialized component instance
     */
    public Command getBACK_ChonAbout() {
        if (BACK_ChonAbout == null) {//GEN-END:|54-getter|0|54-preInit
            // write pre-init user code here
            BACK_ChonAbout = new Command("Back", Command.BACK, 0);//GEN-LINE:|54-getter|1|54-postInit
            // write post-init user code here
        }//GEN-BEGIN:|54-getter|2|
        return BACK_ChonAbout;
    }
    //</editor-fold>//GEN-END:|54-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BACK_ChonLoad ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of BACK_ChonLoad component.
     * @return the initialized component instance
     */
    public Command getBACK_ChonLoad() {
        if (BACK_ChonLoad == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            BACK_ChonLoad = new Command("Back", Command.BACK, 0);//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return BACK_ChonLoad;
    }
    //</editor-fold>//GEN-END:|56-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BACK_ChonOption ">//GEN-BEGIN:|58-getter|0|58-preInit
    /**
     * Returns an initiliazed instance of BACK_ChonOption component.
     * @return the initialized component instance
     */
    public Command getBACK_ChonOption() {
        if (BACK_ChonOption == null) {//GEN-END:|58-getter|0|58-preInit
            // write pre-init user code here
            BACK_ChonOption = new Command("Back", Command.BACK, 0);//GEN-LINE:|58-getter|1|58-postInit
            // write post-init user code here
        }//GEN-BEGIN:|58-getter|2|
        return BACK_ChonOption;
    }
    //</editor-fold>//GEN-END:|58-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceFileSave ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of choiceFileSave component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceFileSave() {
        if (choiceFileSave == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            choiceFileSave = new ChoiceGroup("Ch\u1ECDn file \u0111\u1EC3 load", Choice.EXCLUSIVE);//GEN-LINE:|61-getter|1|61-postInit
            // write post-init user code here
            SaveManager.OpenDB();

            FileSaveList = SaveManager.GetSaveList();
            Enumeration en = FileSaveList.keys();
            while(en.hasMoreElements())
            {
                String strSaveName = (String)en.nextElement();
                choiceFileSave.append(strSaveName, null);
            }

            SaveManager.CloseDB();
        }//GEN-BEGIN:|61-getter|2|
        return choiceFileSave;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    public ChoiceGroup newChoiceFileSave()
    {
        choiceFileSave = new ChoiceGroup("Ch\u1ECDn file \u0111\u1EC3 load", Choice.EXCLUSIVE);                                   
        SaveManager.OpenDB();

        FileSaveList = SaveManager.GetSaveList();
        Enumeration en = FileSaveList.keys();
        while(en.hasMoreElements())
        {
            String strSaveName = (String)en.nextElement();
            choiceFileSave.append(strSaveName, null);
        }

        SaveManager.CloseDB();
        return choiceFileSave;
    }
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OK_ChonOption ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of OK_ChonOption component.
     * @return the initialized component instance
     */
    public Command getOK_ChonOption() {
        if (OK_ChonOption == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            OK_ChonOption = new Command("Ok", Command.OK, 0);//GEN-LINE:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return OK_ChonOption;
    }
    //</editor-fold>//GEN-END:|62-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OK_ChonLoad ">//GEN-BEGIN:|66-getter|0|66-preInit
    /**
     * Returns an initiliazed instance of OK_ChonLoad component.
     * @return the initialized component instance
     */
    public Command getOK_ChonLoad() {
        if (OK_ChonLoad == null) {//GEN-END:|66-getter|0|66-preInit
            // write pre-init user code here
            OK_ChonLoad = new Command("Ok", Command.OK, 0);//GEN-LINE:|66-getter|1|66-postInit
            // write post-init user code here
        }//GEN-BEGIN:|66-getter|2|
        return OK_ChonLoad;
    }
    //</editor-fold>//GEN-END:|66-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        if(m_playSound != null)
        {
            try
            {
                m_playSound.stop();
                m_playSound.close();
                m_playSound.deallocate();
            }
            catch(Exception ex)
            {

            }
        }
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public void PlaySound()
    {
        if(m_playSound != null)
        {
            try
            {
                m_playSound.stop();
                m_playSound.close();
            }
            catch(Exception ex)
            {

            }
        }
        if(isPlaying)
        {
            try
            {
                m_playSound = Manager.createPlayer(getClass().getResourceAsStream("/playing.mid"), "audio/midi");
                m_playSound.setLoopCount(-1); // play indefinitely
                m_playSound.prefetch(); // prefetch
                m_playSound.start();
            }
            catch(Exception ex)
            {

            }
        }
        else
        {
            try
            {
                m_playSound = Manager.createPlayer(getClass().getResourceAsStream("/start.mid"), "audio/midi");
                m_playSound.setLoopCount(-1); // play indefinitely
                m_playSound.prefetch(); // prefetch
                m_playSound.start();
            }
            catch(Exception ex)
            {

            }
        }
    }
}
